﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Data.Common;

public partial class TimeZoneSet : System.Web.UI.Page
{
    Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase("Application_Data");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DDLTimeZone.DataSource = TimeZoneInfo.GetSystemTimeZones();
            DDLTimeZone.DataValueField = "Id";
            DDLTimeZone.DataTextField = "DisplayName";
            DDLTimeZone.DataBind();

            this.lblLogin.Text = Session["sLoginDomain"].ToString() + @"\" + Session["sLogin"].ToString();
            this.lblFullName.Text = Session["sFullUserName"].ToString();
            this.lblEMailAddress.Text = Session["sEMailAddress"].ToString();
        }       

    }
    protected void DDLTimeZone_SelectedIndexChanged(object sender, EventArgs e)
    {
         String strUserCount = db.ExecuteScalar(CommandType.Text, "SELECT Count(*) FROM [User]").ToString();
         
        if (strUserCount =="0")
        {
            DbCommand dbCommand = db.GetSqlStringCommand("INSERT INTO [User](UserDomain,UserNetworkLogin,UserFullName,UserEMailAddress,IsAdmin,TimeZone,TimeZoneDisplayName) VALUES(" +
                                                         "'" + Session["sLoginDomain"].ToString() + "'," +
                                                         "'" + Session["sLogin"].ToString() + "'," +
                                                         "'" + Session["sFullUserName"].ToString() + "'," +
                                                         "'" + Session["sEMailAddress"].ToString() + "'," +
                                                         "'True'," +
                                                         "'" + this.DDLTimeZone.SelectedValue.ToString() + "'," +
                                                         "'" + this.DDLTimeZone.SelectedItem.ToString() + "')");

            db.ExecuteNonQuery(dbCommand);
            Session["sTimeZone"] = this.DDLTimeZone.SelectedItem.ToString();
            Session["sAdministrator"] = true;
            Session["sRole"] = "Administrator";
            Response.Redirect(Session["sPreviousPage"].ToString());
        }
        else
        {
            DbCommand dbCommand = db.GetSqlStringCommand("INSERT INTO [User](UserDomain,UserNetworkLogin,UserFullName,UserEMailAddress,IsAdmin,TimeZone,TimeZoneDisplayName) VALUES(" +
                                                         "'" + Session["sLoginDomain"].ToString() + "'," +
                                                         "'" + Session["sLogin"].ToString() + "'," +
                                                         "'" + Session["sFullUserName"].ToString() + "'," +
                                                         "'" + Session["sEMailAddress"].ToString() + "'," +
                                                         "'False'," +
                                                         "'" + this.DDLTimeZone.SelectedValue.ToString() + "'," +
                                                         "'" + this.DDLTimeZone.SelectedItem.ToString() + "')");

            db.ExecuteNonQuery(dbCommand);
            Session["sTimeZone"] = this.DDLTimeZone.SelectedItem.ToString();
            Response.Redirect(Session["sPreviousPage"].ToString());
        }    
    }
}
